<?php 
class SeasonsController extends AppController
{

    var $name = 'Seasons';
    var $helpers = array(
        'Html', 'Form'
    );
    var $uses = array('Season', 'Event');

	function beforeFilter(){ 
        $this->__validateLoginStatus();
    }

    function __validateLoginStatus(){ 
       if($this->Session->check('User') == false){
          $this->Session->setFlash('You must be logged in to access this page');
          $this->redirect(array('controller' => 'users', 'action' => 'login'));
       }
    }

    function index()
    {
        $this->Season->recursive = 0;
        $this->set('seasons', $this->paginate());
    }

    function view($id = null)
    {
        if (!$id)
        {
            $this->Session->setFlash(__('Invalid Season', true));
            $this->redirect(array(
                'action'=>'index'
            ));
        }
        $season = $this->Season->find('first', 
        					array(
        					'conditions' => array(
        						'Season.id' => $id
        						),
        					'fields' => array(
        						'Season.id',
        						'Season.start_date',
        						'Season.end_date',
        						'Season.season_name',
        						'Season.description'
        						),
        					'recursive' => -1
        					)
        				);
		//To check the season is exist 	       				
        if (!$season) {
        	$this->Session->setFlash(__('Invalid Season', true));
        	$this->redirect(array('action' => 'index'));
        }
        $events = $this->Event->find('all',
        				array(
        					'conditions' => array(
        						'Event.season_id' => $id
        					),
        					'fields' => array(
        						'Event.id',
        						'Event.title'
        					),
        					'recursive' => -1
        				)
        	);
        	
        $this->set('season', $season);
        $this->set('events', $events);
        $this->set('accessArray', $this->Session->read('accessArray'));
    }

    function add()
    {
    	$this->__mainValidate('CreateSeason');	
        if (! empty($this->data))
        {
            $this->Season->create();
            if ($this->Season->save($this->data))
            {
                $this->Session->setFlash(__('The Season has been saved', true));
                $this->redirect(array(
                    'action'=>'index'
                ));
            }
            else
            {
                $this->Session->setFlash(__('The Season could not be saved. Please, try again.', true));
            }
        }
    }
    
    function edit($id = null)
    {
    	$this->__mainValidate('CreateSeason');
        if (!$id && empty($this->data))
        {
            $this->Session->setFlash(__('Invalid Season', true));
            $this->redirect(array(
                'controller' => HOME_CONTROLLER, 'action'=>'index'
            ));
        }
        
        //check that $id is a valid Season
        $this->__checkValidSeason($id);
        			
        if (! empty($this->data))
        {
            if ($this->Season->save($this->data))
            {
                $this->Session->setFlash(__('The Season has been saved', true));
                $this->redirect(array(
                    'action'=>'view', $id
                ));
            }
            else
            {
                $this->Session->setFlash(__('The Season could not be saved. Please, try again.', true));
                 $this->data = $this->Season->read(null, $id);
            }
        }
        if ( empty($this->data))
        {
            $this->data = $this->Season->read(null, $id);
        }
    }
    
    function delete($id = null)
    {
    	$this->__mainValidate('CreateSeason');
        if (!$id)
        {
            $this->Session->setFlash(__('Invalid id for Season', true));
            $this->redirect(array(
                'action'=>'index'
            ));
        }
        
        //check that $id is a valid Season
        $this->__checkValidSeason($id);
        
        if ($this->Season->del($id))
        {
            $this->Session->setFlash(__('Season deleted', true));
            $this->redirect(array(
                'action'=>'index'
            ));
        }
        $this->Session->setFlash(__('The Season could not be deleted. Please, try again.', true));
        $this->redirect(array(
            'action'=>'view', $id
        ));
    }

    
    function __checkValidSeason($id) {
        //check that $id is a valid Season
        $season = $this->Season->find('first', 
        			array(
        				'conditions' => array(
        					'Season.id' => $id
        					),
        				'fields' => array(
        					'Season.id', 'Season.season_name'
        					)
        				)
        			);
        if (!$season) {
        	$this->Session->setFlash(__('Invalid Season id.', true));
            $this->redirect(array(
                'controller' => HOME_CONTROLLER, 'action'=>'index'
            ));
        }
    }
}
?>
